package com.imagination.applet.domain.adapter.os;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import com.imagination.applet.domain.adapter.ObjectStorageAdapter;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.InputStream;

@Component
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class OSSAdapter implements ObjectStorageAdapter {

    private final OSS client;
    @Value("${aliyun.oss.bucket-name}")
    private String bucketName;


    private final static ObjectMetadata metadata;

    static {
        metadata = new ObjectMetadata();
        metadata.addUserMetadata("Content-Disposition", "inline");
    }

    @Override
    public void putObject(String key, InputStream in) throws Exception {
        client.putObject(bucketName, key, in, metadata);
    }

    public String getObject(String key) {
        OSSObject result = client.getObject(bucketName, key);
        return result.getResponse().getUri();
    }

}
